package dbgroup02.dao.db2;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import dbgroup02.bean.FlightRoute;
import dbgroup02.dao.FlightRouteDao;

public class FlightRouteDB2Dao implements FlightRouteDao {

	private JdbcTemplate jdbcTemplate;

	ParameterizedRowMapper<FlightRoute> getFlightRouteMapper = new ParameterizedRowMapper<FlightRoute>() {
		@Override
		public FlightRoute mapRow(ResultSet resultSet, int arg1)
				throws SQLException {
			FlightRoute flightRoute = new FlightRoute();
			flightRoute.setIdFlightRoute(resultSet.getInt(1));
			flightRoute.setRouteKM(resultSet.getInt(2));
			flightRoute.setFromAirport(resultSet.getString(3));
			flightRoute.setToAirport(resultSet.getString(4));
			return flightRoute;
		}
	};

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public FlightRoute getFlightRoute(int idFlightRoute) {
		List<FlightRoute> result = jdbcTemplate.query("SELECT * FROM "
				+ FlightRouteDao.SCHEMA
				+ ".FLIGHT_ROUTE WHERE FLIGHT_ROUTE_ID = ?",
				new Object[] { idFlightRoute }, getFlightRouteMapper);

		if (result.size() > 0)
			return result.get(0);
		else
			return null;
	}

}
